/*
** main.c for ololol in /home/phand/exotest/tek2/prog-elem/trade
** 
** Made by david phan
** Login   <phan_d@epitech.net>
** 
** Started on  Tue Apr 17 16:38:24 2012 david phan
** Last update Fri Jun  1 16:57:19 2012 david phan
*/

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define BUFFER_SIZE	512

typedef struct	s_trade
{
  int		start_money;
  int		current_money;
  int		total_days;
  int		current_day;
  int		current_auction;
}		t_trade;

static void	init_trade(t_trade *trade)
{
  char		buff[BUFFER_SIZE];
  char		*sub;
  int		end;

  bzero(buff, BUFFER_SIZE);
  end = read(0, buff, BUFFER_SIZE);
  buff[end];

  sub = strtok(buff, "\n");
  trade->start_money = atoi(sub);
  sub = strtok(NULL, "\n");
  trade->total_days = atoi(sub);
  sub = strtok(NULL, "\n");
  trade->current_auction = atoi(sub);
  trade->current_money = trade->start_money;
  trade->current_day = 1;
}

static void	do_the_magic(t_trade *trade)
{
  if (trade->current_day == 1)
    write(1, "buy 50\n", strlen("buy 50\n"));
  else if (trade->current_day == trade->total_days - 1)
    write(1, "sell 50\n", strlen("sell 50\n"));
  else
    write(1, "passe\n", strlen("passe\n"));
}

int		main()
{
  t_trade	trade;
  char		buff[BUFFER_SIZE];
  int		end;
  int		fd;

  fd = open("result.txt", O_CREAT | O_RDWR | O_TRUNC, 0644);
  init_trade(&trade);
  while (trade.current_day < trade.total_days)
  {
    do_the_magic(&trade);
    end = read(0, buff, BUFFER_SIZE);
    buff[end] = '\0';
    trade.current_day++;
  }
  close(fd);
}
